package main

import (
	"context"
	"fmt"
	pb "grpcStudy/server/proto"
	"net"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
)

type server struct {
	pb.UnimplementedSayHelloServer
}

// 重写SayHello方法
func (s *server) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloResponse, error) {
	return &pb.HelloResponse{ResponseMsh: "hello" + req.RequestName}, nil
}

func main() {
	// TSL认证
	// 两个参数分别是 cretFile, keyFile
	creds, _ := credentials.NewServerTLSFromFile("D:\\code\\go\\grpcStudy\\key\\test.pem", "D:\\code\\go\\grpcStudy\\key\\test.key")
	// 开启端口
	listen, _ := net.Listen("tcp", ":9090")
	// 创建grpc服务
	grpcServer := grpc.NewServer(grpc.Creds(creds))
	// 向grpc服务端中注册自己grpc服务
	pb.RegisterSayHelloServer(grpcServer, &server{})
	// 启动服务
	err := grpcServer.Serve(listen)
	if err != nil {
		fmt.Printf("failed to serve: %v", err)
		return
	}
}
